<?php

class Admin_Table_Objects extends Apk_Db_Table
{
	protected $_name      = 'objects';
    protected $_primary   = 'id';
    protected $_ERROR = '';

    public function GetError()
    {
    	return $this->_ERROR;
    }

    public function GetDataList($page=1, $sort=null, $page_count=null, $params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name, array('id', 'path', 'title', 'add_date', 'is_active', 'file_name', 'norder'))
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0');

    	if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:
    				$sel->where($param.' = ?', $val);
    				break;
    		}
    	}

		if(!is_null($sort) && strlen($sort) > 0) $sel->order($this->_name.'.'.$sort);
		else $sel->order($this->_name.'.norder');

    	return $this->setDecorator('listDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetDataParamsList($params=null)
    {
    	$sel = $this->getAdapter()->select()
    		  ->from($this->_name)
    		  ->where($this->_name.'.is_ghost = 0')
    		  ->where($this->_name.'.is_removed = 0')
    		  ->order($this->_name.'.norder');

    	if(!is_null($params)){
    		foreach($params as $param => $val)
    		switch ($param)
    		{
    			default:
    				$sel->where($param.' = ?', $val);
    				break;
    		}
    	}
    	return $this->setDecorator('simpleDecorator')->getList($sel)->toArray();
    }

    public function GetDataRow($id)
    {
    	$sel = $this->getAdapter()->select()
    				->from($this->_name)
    				->where('id = ?', $id);

    	return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }

	public function insert(array $data)
    {
    	$data['add_user_id'] = Zend_Registry::get('auth')->getIdentity()->id;
    	$data['add_date'] = time();
    	$data['modify_date'] = time();

        return parent::insert($data);
    }

    public function update(array $data, $id)
    {
    	$data['modify_user_id'] = Zend_Registry::get('auth')->getIdentity()->id;
    	$data['modify_date'] = time();
    	$data['is_ghost'] = 0;

    	try{
		return parent::update($data, 'id = '.$id);
    	}catch(Exception $e)
    	{
    		echo $e->__toString();
    		die();
    	}
    }

    public function remove($items, $user_id)
    {
    	foreach($items as $k => $id)
    	{
	    	$data['modify_date'] = time();
	    	$data['is_removed'] = 1;

	    	parent::update($data, 'id = '.$id);
    	}

    	return true;
	}

	public function gorder($id, $gid, $d, $param = 'id_gallery')
    {
    	$sel = $this->getAdapter()->select()
    					->from($this->_name)
    					->where($param.' = ?', $gid)
    					->order('norder');

    	$data = $this->getList($sel)->toArray();

    	foreach($data as $k => $v)
    	{
    		if($v['id'] == $id)
    		{
    			if($d == 1){
    				parent::update(array('norder' => $k-1), 'id = '.$v['id']);
    				parent::update(array('norder' => $k), 'id = '.$data[$k-1]['id']);
    				break;
    			}

    			if($d == 2){
    				parent::update(array('norder' => $k+1), 'id = '.$v['id']);
    				parent::update(array('norder' => $k), 'id = '.$data[$k+1]['id']);
    				break;
    			}
    		}

    		parent::update(array('norder' => $k), 'id = '.$v['id']);
    	}
    }

public function order_exif($id, $param = 'id_gallery')
    {
    	$sel = $this->getAdapter()->select()
    					->from($this->_name)
    					->where($param.' = ?', $id)
    					->where('is_active = 1')
    					->where('is_ghost = 0')
    					->where('is_removed = 0');

    	$data = $this->getList($sel)->toArray();

    	foreach($data as $k => $v)
    	{
    		parent::update(array('norder' => $v['exif_DateTimeInt']), 'id = '.$v['id']);
    	}
    }

    public function order_adddate($id, $param = 'id_gallery')
    {
    	$sel = $this->getAdapter()->select()
    					->from($this->_name)
    					->where($param.' = ?', $id)
    					->where('is_active = 1')
    					->where('is_ghost = 0')
    					->where('is_removed = 0');

    	$data = $this->getList($sel)->toArray();

    	foreach($data as $k => $v)
    	{
    		parent::update(array('norder' => $v['add_date']), 'id = '.$v['id']);
    	}
    }

    public function order_dragsort($list = '')
    {
    	$data = explode('|', $list);

    	if(count($data) > 1)
    	foreach($data as $k => $id)
    	{
    		parent::update(array('norder' => ($k+1)), 'id = '.$id);
    	}
    }

    /********************
     ***  Decorators  ***
    *********************/

    public function simpleDecorator($key, $value)
    {
        switch ($key) {
            case 'modify_date':
            case 'add_date':
            	$result[$key]['__attributes'] = $this->timestampToArray($value);
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

    public function listDecorator($key, $value)
    {
        switch ($key) {
            case 'public_date':
            case 'modify_date':
            case 'add_date':
            	$result[$key] = date('d-m-Y');
            	break;
            default:
                $result[$key] = stripslashes($value);
                break;
        }
        return $result;
    }

}
?>